﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MonitorFileConvert
{
    public partial class GetSwf : Form
    {
        public GetSwf()
        {
            InitializeComponent();
            GetFileExtends();
        }
        private readonly static string EXE_PATH = Application.StartupPath + (@"\pdf2swf.exe");
        private List<string> list;
        /// <summary>
        /// 获取文件扩展名列表
        /// </summary>
        /// <returns></returns>
        private List<string> GetFileExtends()
        {
            list = new List<string>();
            //if (this.checkDoc.Checked)
            //    list.Add(".flv");
            //if (this.checkXls.Checked)
            //    list.Add(".mp4");
            //if (this.checkWmv.Checked)
            //    list.Add(".wmv");
            //if (this.checkMpeg.Checked)
            //    list.Add(".mpeg");
            //if (this.checkAvi.Checked)
            //    list.Add(".avi");
            //if (this.check3GP.Checked)
            //    list.Add(".3gp");
            //if (this.checkMpg.Checked)
            //    list.Add(".mpg");
            list.Add(".pdf");
            return list;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _BeginCreateSwfFile(this.textFilesPath.Text.Trim());
        }

        private void _BeginCreateSwfFile(string directoryPath)
        {
            System.IO.DirectoryInfo direct = new System.IO.DirectoryInfo(directoryPath);
            foreach (System.IO.DirectoryInfo dir in direct.GetDirectories())
            {
                _BeginCreateSwfFile(dir.FullName);
            }
            foreach (System.IO.FileInfo file in direct.GetFiles())
            {
                CreateSwfFile(file);
            }
            
        }

        private void CreateSwfFile(System.IO.FileInfo file)
        {
            if (list.Contains(file.Extension.Trim().ToLower()))
            {
                //
                string stringTempPath = file.FullName.ToLower().Replace(".pdf", "_1.swf");
                string strCmdInfo = String.Format("\"{0}\" -o \"{1}\" -f -T 6 -p 1", file.FullName, stringTempPath);
                if (!System.IO.File.Exists(stringTempPath))
                {
                    FileConvertCmd(strCmdInfo);
                    this.textMemo.Text += file.FullName+"\r\n";
                }
            }
        }

        #region 调用批处理程序
        /// <summary>
        /// 调用批处理程序
        /// </summary>
        /// <param name="stringPath">可执行文件存放地址</param>
        /// <param name="stringArguments">命令参数</param>
        /// <returns></returns>
        private static bool FileConvertCmd(string stringArguments)
        {
            if (!System.IO.File.Exists(EXE_PATH))
                return false;

            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(EXE_PATH);
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

            startInfo.Arguments = " " + stringArguments;

            try
            {
                //Helper.AddLogFiles(EXE_PATH + " " + startInfo.Arguments);

                System.Diagnostics.Process.Start(startInfo);
                return true;
            }
            catch (Exception ex)
            {
               // Helper.AddLogFiles(EXE_PATH + " " + startInfo.Arguments + " Error:" + ex.Message);
                return false;
            }
        }
        #endregion


    }
}
